package com.zoo.curator.impl;

import com.jz.bean.AppInfoBean;
import com.jz.util.TimeUtil;
import com.zoo.curator.AbstractCurator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.state.ConnectionState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2015/10/29.
 * 主备模式
 */
public class MasterCurator extends AbstractCurator {
    private static Logger loggerChild = LoggerFactory.getLogger("child");
    public MasterCurator(String execStr) {
        super(execStr, true);
    }

    @Override
    public void startProcess(PathChildrenCacheEvent event) {
        try {
            if (exec == null) {
                return;
            }
            if (child != null) {
                return;
            }
            if (!leaderSelector.isLeader()) {
                LOGGER.info("未获得LEADER权,不启动process");
                return;
            }
            child = Runtime.getRuntime().exec(exec);
            updateAppNodeValue(new AppInfoBean(true, TimeUtil.getTime(), "CHILD start"));
            LOGGER.info("开始监控child PROCESS");
            String temp = "";
            try {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(child.getInputStream()));
                while ((temp = bufferedReader.readLine()) != null)
                    loggerChild.info(temp);
            } catch (IOException e) {
                e.printStackTrace();
            }
            child.waitFor();
            LOGGER.info("child process自行退出,置为NULL");
        } catch (Exception e) {
            LOGGER.error("child process执行异常", e);
            updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop error"));
        } finally {
            updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop"));
            child = null;
        }
    }

    @Override
    public void killProcess(PathChildrenCacheEvent event) {
        if (child != null) {
            LOGGER.info("Killing child PROCESS");
            try {
                child.destroy();
                LOGGER.info("child process停止运行");
            } catch (Exception e) {
                LOGGER.info("child process停止运行异常");
                updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop error"));
            } finally {
                child = null;
                updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop"));
            }
        }
    }

    public static void main(String[] args) {
        new MasterCurator(null);
    }

    @Override
    public void run() {
        try {
            while (true) {
//                Thread.sleep(100);
            }
        } catch (Exception e) {
        }
    }


    @Override
    public void watchCurator(CuratorFramework client, CuratorEvent event) {

    }

    @Override
    public void watchConnectState(CuratorFramework client, ConnectionState state) throws Exception {

    }

    @Override
    public void watchMainNodeData(NodeCache nodeCache) {
        System.out.println("Node data is changed, new data: " +
                new String(nodeCache.getCurrentData().getData()));
    }

    @Override
    public void watchAppNodeData(NodeCache nodeCache) {

    }
}
